package com.wyw.minio.learning.service.impl;

import com.wyw.minio.learning.config.MinioProperties;
import cn.hutool.core.util.StrUtil;
import com.wyw.minio.learning.service.MinioClientService;
import com.wyw.minio.learning.utils.MinioClientUtils;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @Title
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2024/3/28 16:58
 * Update History:
 * Author        Time            Content
 */
@Service
@Slf4j
public class MinioClientServiceImpl implements MinioClientService {

    private final MinioClientUtils minioClientUtils;
    private final MinioProperties minioProperties;

    public MinioClientServiceImpl(MinioClientUtils minioClientUtils, MinioProperties minioProperties) {
        this.minioClientUtils = minioClientUtils;
        this.minioProperties = minioProperties;
    }

    @Override

    public boolean bucketExists(String bucketName) {
        return minioClientUtils.bucketExists(bucketName);
    }

    @Override
    public void makeBucket(String bucketName) {
        minioClientUtils.makeBucket(bucketName);
    }


    @Override
    public void setPolicy(String bucketName) {
        minioClientUtils.setBucketPolicy(bucketName);
    }

    @Override
    public List<String> listBucketName() {
        return minioClientUtils.listBucketNames();
    }

    @Override
    public List<Bucket> listBuckets() {
        return minioClientUtils.listBuckets();
    }

    @Override
    public boolean removeBucket(String bucketName) {
        return minioClientUtils.removeBucket(bucketName);
    }

    @Override
    public List<String> listObjectNames(String bucketName) {
        return minioClientUtils.listObjectNames(bucketName);
    }

    @Override
    public String putObject(MultipartFile file, String bucketName, String fileType) {
        try {
            bucketName = StrUtil.isNotBlank(bucketName) ? bucketName : minioProperties.getBucketName();
            if (!this.bucketExists(bucketName)) {
                this.makeBucket(bucketName);
            }
            String fileName = file.getOriginalFilename();

            String objectName = UUID.randomUUID().toString().replaceAll("-", "")
                    + fileName.substring(fileName.lastIndexOf("."));
            minioClientUtils.putObject(bucketName, file, objectName, fileType);
            return minioProperties.getEndpoint() + "/" + bucketName + "/" + objectName;
        } catch (Exception e) {
            log.error("上传文件异常", e);
            return "上传失败";
        }
    }

    @Override
    public InputStream downloadObject(String bucketName, String objectName) {
        return minioClientUtils.getObject(bucketName, objectName);
    }

    @Override
    public boolean removeObject(String bucketName, String objectName) {
        return minioClientUtils.removeObject(bucketName, objectName);
    }

    @Override
    public boolean removeListObject(String bucketName, List<String> objectNameList) {
        return minioClientUtils.removeObject(bucketName, objectNameList);
    }

    @Override
    public String getObjectUrl(String bucketName, String objectName) {
        return minioClientUtils.getObjectUrl(bucketName, objectName);
    }
}
